<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="承租方名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.lesseeName`"
            :rules="formRules.lesseeName"
            class="mb-0px!"
          >
            <el-input v-model="row.lesseeName" placeholder="请输入承租方名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="承租方类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.lesseeType`"
            :rules="formRules.lesseeType"
            class="mb-0px!"
          >
            <el-select v-model="row.lesseeType" placeholder="请选择承租方类型">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.OBLIGEE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="承租方证件类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.lesseeCertificateType`"
            :rules="formRules.lesseeCertificateType"
            class="mb-0px!"
          >
            <el-select v-model="row.lesseeCertificateType" placeholder="请选择承租方证件类型">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.ID_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="承租方证件号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.lesseeCertificate`"
            :rules="formRules.lesseeCertificate"
            class="mb-0px!"
          >
            <el-input v-model="row.lesseeCertificate" placeholder="请输入承租方证件号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="法人名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.corporationName`"
            :rules="formRules.corporationName"
            class="mb-0px!"
          >
            <el-input v-model="row.corporationName" placeholder="请输入法人名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="法人/单位电话" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.lesseePhone`"
            :rules="formRules.lesseePhone"
            class="mb-0px!"
          >
            <el-input v-model="row.lesseePhone" placeholder="请输入法人/单位电话" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="经办人电话" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.liasionPhone`"
            :rules="formRules.liasionPhone"
            class="mb-0px!"
          >
            <el-input v-model="row.liasionPhone" placeholder="请输入经办人电话" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="地址" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.lesseeLocation`"
            :rules="formRules.lesseeLocation"
            class="mb-0px!"
          >
            <el-input v-model="row.lesseeLocation" placeholder="请输入地址" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="法人证件类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.corporationCertificateType`"
            :rules="formRules.corporationCertificateType"
            class="mb-0px!"
          >
            <el-select v-model="row.corporationCertificateType" placeholder="请选择法人证件类型">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.ID_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="法人证件号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.corporationCertificate`"
            :rules="formRules.corporationCertificate"
            class="mb-0px!"
          >
            <el-input v-model="row.corporationCertificate" placeholder="请输入法人证件号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="经办人" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.lesseeLiasion`"
            :rules="formRules.lesseeLiasion"
            class="mb-0px!"
          >
            <el-input v-model="row.lesseeLiasion" placeholder="请输入经办人" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.status`" :rules="formRules.status" class="mb-0px!">
            <el-radio-group v-model="row.status">
              <el-radio
                v-for="dict in getStrDictOptions(DICT_TYPE.SYS_STATUS)"
                :key="dict.value"
                :value="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button type="danger" @click="handleDelete($index)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-col :span="8">
      <el-select
        clearable
        @change="handleChooseOne"
        v-model="selectedDepeName"
        placeholder="选择现有单位信息"
      >
        <el-option
          v-for="item in allEnterpriseInfoList"
          :key="item.id"
          :label="item.deptName"
          :value="item"
        />
      </el-select>
    </el-col>

    <el-button @click="handleAdd" round>或： 手动添加承租人信息</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { RentApi } from '@/api/zszx/rent'
import { MineInfoApi } from '@/api/zszx/mineinfo'

const selectedDepeName = ref('')
const allEnterpriseInfoList = ref([])
onMounted(async () => {
  allEnterpriseInfoList.value = await MineInfoApi.all()
  console.log('allEnterpriseInfoList', allEnterpriseInfoList.value)
})
const handleChooseOne = (info) => {
  console.log('info', info)
  const row = {
    id: undefined,
    rentId: undefined,
    lesseeName: info.deptName,
    lesseeType: info.deptType,
    lesseeCertificateType: info.deptCertificateType,
    lesseeCertificate: info.deptCertificate,
    corporationName: info.corporationName,
    lesseePhone: info.deptPhone,
    liasionPhone: info.operatorPhone,
    lesseeLocation: info.deptAddress,
    corporationCertificateType: info.corporationCertificateType,
    corporationCertificate: info.corporationCertificate,
    lesseeLiasion: info.operator,
    remark: info.remark,
    status: info.status
  }
  row.rentId = props.rentId
  formData.value.push(row)
}

const props = defineProps<{
  rentId: undefined // 租赁合同序号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  rentId: [{ required: true, message: '租赁合同序号不能为空', trigger: 'blur' }],
  lesseeName: [{ required: true, message: '承租方名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.rentId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      formData.value = await RentApi.getLesseeListByRentId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    rentId: undefined,
    lesseeName: undefined,
    lesseeType: undefined,
    lesseeCertificateType: undefined,
    lesseeCertificate: undefined,
    corporationName: undefined,
    lesseePhone: undefined,
    liasionPhone: undefined,
    lesseeLocation: undefined,
    corporationCertificateType: undefined,
    corporationCertificate: undefined,
    lesseeLiasion: undefined,
    remark: undefined,
    status: undefined
  }
  row.rentId = props.rentId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
